package apptoko.com;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import apptoko.com.util.Constants;

import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMBroadcastReceiver extends BroadcastReceiver {

	static final String TAG = "GCMBroadcastReceiver";
    
    @Override
    public void onReceive(Context context, Intent intent) {
    	Log.e(TAG, "Received message");
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        String messageType = gcm.getMessageType(intent);
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            //sendNotification("Send error: " + intent.getExtras().toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            //sendNotification("Deleted messages on server: " + intent.getExtras().toString());
        } else {
             String message = intent.getExtras().getString("message");
             String title = intent.getExtras().getString("title");
             String linkUrl = intent.getExtras().getString("link_url");
             String store = intent.getExtras().getString("store");
             String contentId = intent.getExtras().getString("content_id");
             
             Bundle data = new Bundle();
             data.putString("message", message);
             data.putString("title", title);
             data.putString("link_url", linkUrl);
             data.putString("store", store);
             data.putString("content_id", contentId);
             sendPushData(context, data);
        }
        setResultCode(Activity.RESULT_OK);
    }

    public void sendPushData(Context context, Bundle data) {
        Intent intent = new Intent(Constants.DISPLAY_MESSAGE_ACTION);
        intent.putExtras(data);
        context.sendBroadcast(intent);
    }
}
